<template>
  <div>
    ==={{ checkList }}==={{ id }}
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
    <el-button size="small" @click="hCancel">取消</el-button>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: '',
  data() {
    return {
      roleList: [],
      checkList: [],
      id: ''
    }
  },
  computed: {},
  created() {
    this.loadRole() // 获取所有的角色
  },
  methods: {
    async loadRole() {
      const res = await getRoles()
      this.roleList = res.data.rows
      this.getCurrentRole() // 获取当前所拥有的角色
    },
    async getCurrentRole() {
      // 获取用户所拥有的角色
      console.log(this.id, 991)
      const res = await getUserDetailById(this.id)
      this.checkList = res.data.roleIds
    },
    async hSubmit() {
      // 分配角色
      const res = await assignRoles({
        id: this.id,
        roleIds: this.checkList
      })
      this.$emit('success')
    },
    hCancel() {
      this.$emit('success')
    }
  }
}
</script>
<style lang="less" scoped></style>
